<!--
 * @Author: JiraiyaChen 979526234@qq.com
 * @Date: 2025-05-19 16:25:05
 * @LastEditors: JiraiyaChen 979526234@qq.com
 * @LastEditTime: 2025-06-04 10:01:25
 * @Description: 
* 评分点表单组件
* 客观分和主观分两种评分模式
* 提供多种4种计算公式（均值算分法、低价优先法、工程方法1、工程方法2），
* 可动态添加/删除评分项
-->

<template>
  <div v-if="loadedFrom" class="relative">
    <!-- {{ formItem }} -->
    <!-- {{ calculationFormItem }} -->
    <!-- {{ formData }} -->
    <!-- {{ showCalculationForm }} -->
    <!-- {{ clFormRef }} -->
    <ClForm
      ref="clFormRef"
      :form-item="formItem"
      label-width="150px"
      :disabled="disabled"
      @on-change-input="reflectChange"
      @on-change-select="onChangeSelect1"
    />
    <template v-if="showCalculationForm">
      <ClForm
        ref="calculationFormRef"
        :form-item="calculationFormItem"
        label-width="150px"
        :disabled="disabled"
        @on-change-input="reflectChange"
        @on-change-select="onChangeSelect2"
      />
      <Method1
        v-show="activeFormula === 'AVG'"
        ref="method1Ref"
        :form-data="apiJsonData1"
        :disabled="disabled"
        @value-change="reflectChange"
      />
      <Method2
        v-show="activeFormula === 'LOW_PRICE'"
        ref="method2Ref"
        :form-data="apiJsonData2"
        :disabled="disabled"
        @value-change="reflectChange"
      />
      <Method3
        v-show="activeFormula === 'METHOD_ONE'"
        ref="method3Ref"
        :form-data="apiJsonData3"
        :disabled="disabled"
        @value-change="reflectChange"
      />
      <Method4
        v-show="activeFormula === 'METHOD_TWO'"
        ref="method4Ref"
        :form-data="apiJsonData4"
        :disabled="disabled"
        @value-change="reflectChange"
      />
    </template>
    <div v-if="showAddBtn" class="absolute right-10 top-1 cl-icon-btn" @click="addItem">
      <SvgIcon icon-class="add" />
    </div>
    <div v-if="showDeleteBtn" class="absolute right-0 top-1 cl-icon-btn" @click="deleteItem">
      <SvgIcon icon-class="delete" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { CALCULATION_FORMULA, SCORE_POINT_FORM_ITEM, SCORE_POINT_FORM_ITEM_2 } from '../data'
import Method1 from './Method1.vue'
import Method2 from './Method2.vue'
import Method3 from './Method3.vue'
import Method4 from './Method4.vue'
const props = defineProps({
  showDeleteBtn: {
    type: Boolean,
    default: true
  },
  showAddBtn: {
    type: Boolean,
    default: false
  },
  formData: {
    type: Object,
    required: true
  },
  disabled: {
    type: Boolean,
    default: false
  },
  markModeCode: {
    type: String,
    default: ''
  }
})
const formItem = ref<any[]>([])
const calculationFormItem = ref(cloneDeep(CALCULATION_FORMULA))
const method1Ref = ref()
const method2Ref = ref()
const method3Ref = ref()
const method4Ref = ref()
const clFormRef = ref()
const calculationFormRef = ref()
const showCalculationForm = ref(false)
const _useEnumeration = useEnumeration()
onMounted(() => {
  init()
})
const loadedFrom = ref(false)
const apiJsonData1 = ref({})
const apiJsonData2 = ref({})
const apiJsonData3 = ref({})
const apiJsonData4 = ref({})
const init = async () => {
  try {
    loadedFrom.value = false
    const [calculateTypeEnum, markScoreTypeEnum]: any = await Promise.all([
      _useEnumeration.getCalculateTypeEnum(),
      _useEnumeration.getMarkScoreTypeEnum()
    ])
    if (props['markModeCode'] === 'DIRECT') {
      // 打分类型: 客观分
      formItem.value = cloneDeep(SCORE_POINT_FORM_ITEM)
    } else if (props['markModeCode'] === 'CONFORM') {
      // 打分类型: 主观分
      formItem.value = cloneDeep(SCORE_POINT_FORM_ITEM_2)
    }

    formItem.value.forEach((item) => {
      if (item.key === 'orderNo') {
        item.value = props.formData['orderNo']
      }
      if (item.key === 'typeCode') {
        item.elementConfig.options = markScoreTypeEnum
        // 符合性打分的时候 评分点类型为 客观分
        item.value = props.markModeCode === 'CONFORM' ? 'OBJECTIVE' : ''
      }
      if (props.formData[item.key] !== undefined) {
        item.value = props.formData[item.key]
      }
    })
    // 设置计算公式下拉
    calculationFormItem.value.forEach((item) => {
      item.value = props.formData[item.key]
      if (item.key === 'calculteCode') {
        item.elementConfig.options = calculateTypeEnum
      }
    })
    // 只有 打分类型 = 自动计算 的时候 才显示计算公式下拉
    if (props.formData['typeCode'] === 'AUTO') {
      showCalculationForm.value = true
    }
    // 设置计算公式数据
    const jsonData = JSON.parse(props.formData['jsonData'] ?? '{}')
    activeFormula.value = props.formData['calculteCode']
    formulaData.value = jsonData
    if (activeFormula.value === 'AVG') {
      apiJsonData1.value = jsonData
    }
    if (activeFormula.value === 'LOW_PRICE') {
      apiJsonData2.value = jsonData
    }
    if (activeFormula.value === 'METHOD_ONE') {
      apiJsonData3.value = jsonData
    }
    if (activeFormula.value === 'METHOD_TWO') {
      apiJsonData4.value = jsonData
    }
    loadedFrom.value = true
  } catch (error) {
    console.log(error)
    loadedFrom.value = true
  }
}

const onChangeSelect1 = (item) => {
  if (item.key === 'typeCode') {
    showCalculationForm.value = item.value === 'AUTO'
  }
  reflectChange()
}
// 计算公式
const activeFormula = ref('')
// 计算公式数据
const formulaData = ref({})
const onChangeSelect2 = (item) => {
  if (item.key === 'calculteCode') {
    activeFormula.value = item.value
    if (item.value === 'AVG') {
      formulaData.value = method1Ref.value.form
    }
    if (item.value === 'LOW_PRICE') {
      formulaData.value = method2Ref.value.form
    }
    if (item.value === 'METHOD_ONE') {
      formulaData.value = method3Ref.value.form
    }
    if (item.value === 'METHOD_TWO') {
      formulaData.value = method4Ref.value.form
    }
  }
  reflectChange()
}

const emit = defineEmits(['deleteItem', 'name-change', 'addItem', 'value-change'])

const deleteItem = () => {
  emit('deleteItem')
}
const addItem = () => {
  emit('addItem')
}
const reflectChange = () => {
  const res = getFormData()
  emit('value-change', res)
}
// 获取表单数据
const getFormData = () => {
  return {
    ...clFormRef.value?.formData,
    ...calculationFormRef?.value?.formData,
    jsonData: JSON.stringify(formulaData.value)
  }
}
const formValidate = async () => {
  try {
    const form1 = await clFormRef?.value?.refElForm?.validate()
    let form2 = true
    let form3 = true // 默认验证通过
    if (showCalculationForm.value) {
      form2 = await calculationFormRef?.value?.refElForm?.validate()
      if (activeFormula.value === 'AVG') {
        form3 = await method1Ref.value.submitForm()
      }
      if (activeFormula.value === 'LOW_PRICE') {
        form3 = await method2Ref.value.submitForm()
      }
      if (activeFormula.value === 'METHOD_ONE') {
        form3 = await method3Ref.value.submitForm()
      }
      if (activeFormula.value === 'METHOD_TWO') {
        form3 = await method4Ref.value.submitForm()
      }
    }
    if (form1 && form2 && form3) {
      return true
    }
    return false
  } catch (error) {
    console.log('🐷 🐽 formValidate 🐽 error👉👉👉', error)
    return false
  }
}
defineExpose({
  getFormData,
  formValidate
})
</script>
